package com.study.practice.structAlogrithm;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {

        ArrayQueue arr = new ArrayQueue(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show)：显示队列");
            System.out.println("e(exit)：退出");
            System.out.println("a(add)：添加数据");
            System.out.println("g(get)：获取数据");
            System.out.println("h(head)：展示队列头数据");

            key = scanner.next().charAt(0); //接受一个字符
            switch (key) {
                case 's':
                    arr.showQueue();
                    break;
                case 'e':
                    loop = false;
                    System.out.println("退出输入");
                    break;
                case 'a':
                    System.out.println("请输入一个数：");
                    int value = scanner.nextInt();
                    arr.addData(value);
                    break;
                case 'g':
                    try {
                        System.out.println("获取的数字为：" + arr.getData());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        arr.peekQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    System.out.println("输入类型有误，请重输。");
                    break;
            }
        }
    }
}

class ArrayQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] array;

    public ArrayQueue(int maxSize) {
        this.front = 0; //执行队列前的数据的前一个位置，不包含数据的下标
        this.rear = 0;  //指向最后一个数据，包含数据的下标
        this.maxSize = maxSize;
        this.array = new int[maxSize];
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public void addData(int data) {
        if (isFull()) {
            System.out.println("queue full");
            return;
        }
        array[rear] = data;
        rear = (rear + 1) % maxSize;
    }

    public int getData() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能取数据");
        }
        int value  = array[front];
        front = (front + 1) % maxSize;
        return value;
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        int validSize = validSize();
        int start = front;
        for (int i = 0; i < validSize; i++) {
            System.out.printf("%d\t", array[start]);
            start = (start + 1) % maxSize;
        }
    }

    public int validSize() {
        return (rear - front + maxSize) % maxSize;
    }

    public void peekQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，无法查询头数据");
        }
        System.out.printf("head data is %d\n", array[front]);
    }


}
